import { useModel } from "umi";
import { message } from "antd";

const useDeleteDepartment = () => {
    const { graphqlFetch } = useModel("global");

    const deleteDepartment = async (oiUuid, callback) => {
        let query = `query MyQuery($oiUuid: UUID!) { a: allOrganizationInstitutionsMembers(condition: { oiUuid: $oiUuid }) { totalCount } }`;
        let res = await graphqlFetch({ query, variables: { oiUuid } });
        if (res === null) return;
        if (res?.a?.totalCount === 0) {
            query = `mutation MyMutation($oiUuid: UUID!) { deleteOrganizationInstitutionByOiUuid(input: { oiUuid: $oiUuid }) { clientMutationId } }`;
            let res = await graphqlFetch({ query, variables: { oiUuid } });
            if (res === null) return;
            message.success("成功删除部门信息");
            callback && callback();
        } else {
            message.warning("该部门下有成员，不能删除");
        }
    };

    return { deleteDepartment };
};

export default useDeleteDepartment;
